﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using DAO;
using BUS;
using System.Threading;

namespace giaodien
{
    public partial class QLKH : Form
    {
        public QLKH()
        {
            InitializeComponent();
        }

        DataSet ds = new DataSet();
        DataSet dsp = new DataSet();
        int f;
        khachhang_DTO kh = new khachhang_DTO();

        private void splitContainer1_Panel1_MouseUp(object sender, MouseEventArgs e)
        {
            dichchuyen.MouseUp();
        }

        private void splitContainer1_Panel1_MouseMove(object sender, MouseEventArgs e)
        {
            dichchuyen.MouseMove(this, MousePosition.X, MousePosition.Y);
        }

        private void splitContainer1_Panel1_MouseDown(object sender, MouseEventArgs e)
        {
            dichchuyen.MouseDown(e);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            GDChinh frm = new GDChinh();
            DialogResult d = MessageBox.Show("Bạn Có Muốn Thoát", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (d == DialogResult.Yes)
            {
                this.Close();
                frm.Show();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void txtsdt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) == false && char.IsControl(e.KeyChar) == false)
            {
                e.Handled = true;
            }
        }

        private void txtcmnd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) == false && char.IsControl(e.KeyChar) == false)
            {
                e.Handled = true;
            }
        }

        private void cbbgioitinh_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void cbbmap_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        public string taomatudong(string automakh)
        {
            string laymalonnhathientai = "";
            int i = 0;
            ds = khachhang_BUS.automakh();
            if (ds.Tables[0].Rows.Count != 0)
            {
                laymalonnhathientai = ds.Tables[0].Rows[0][0].ToString();
                i = int.Parse(laymalonnhathientai.Substring(2, 3).ToString());
                i++;
                if (i < 10)
                    return "KH00" + i.ToString();
                else
                    return "KH0" + i.ToString();
            }
            else
            {
                return "KH001";
            }
        }

        private void trangthainut(Boolean n, Color c)
        {
            nutxoakh.Enabled = n;
            nutsuakh.Enabled = n;
            nutluukh.Enabled = n;
            dtngaysinh.Enabled = n;
            dtngaydat.Enabled = n;
            cbbgioitinh.Enabled = n;
            nutxoakh.BackColor = c;
            nutsuakh.BackColor = c;
            nutluukh.BackColor = c;
        }

        private void trangthaitextbox(Boolean t)
        {
            txttenkh.ReadOnly = t;
            txtsdt.ReadOnly = t;
            txtcmnd.ReadOnly = t;
        }

        private void lammoi()
        {
            lblkh.Text = "";
            txttenkh.Text = "";
            dtngaysinh.Text = "";
            cbbgioitinh.SelectedIndex = -1;
            txtsdt.Text = "";
            txtcmnd.Text = "";
            dtngaydat.Text = "";
            txttenkh.Focus();
            txtmap.Text="";
        }

        void hienthilistview(DataSet ds,ListView danhsach)
        {
            danhsach.Items.Clear();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                ListViewItem lvw = new ListViewItem();
                lvw.Text = ds.Tables[0].Rows[i][0].ToString();
                for (int j = 1; j < ds.Tables[0].Columns.Count; j++)
                {
                    if (ds.Tables[0].Rows[i][j].ToString() != "")
                    {
                        if (j == 3 && j == 7)
                        {
                            lvw.SubItems.Add(string.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(ds.Tables[0].Rows[i][3].ToString()).ToShortDateString()));
                            lvw.SubItems.Add(string.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(ds.Tables[0].Rows[i][7].ToString()).ToShortDateString()));
                        }
                        else
                            lvw.SubItems.Add(ds.Tables[0].Rows[i][j].ToString());
                    }
                    else
                        lvw.SubItems.Add("");
                }
                danhsach.Items.Add(lvw);
            }
        }

        private void QLKH_Load(object sender, EventArgs e)
        {
            lammoi();
            trangthaitextbox(true);
            trangthainut(false, Color.Black);
            ds = khachhang_BUS.laythongtinkh();
            hienthilistview(ds,lvwkh);
            dsp = phong_BUS.loadphong();
        }

        private void nutsuakh_Click(object sender, EventArgs e)
        {
            f = 2;
            trangthainut(false, Color.Black);
            nutluukh.Enabled = true;
            nutluukh.BackColor = Color.Green;
            dtngaysinh.Enabled = true;
            dtngaydat.Enabled = true;
            cbbgioitinh.Enabled = true;
            trangthaitextbox(false);
            txttenkh.Focus();
        }

        private void nutluukh_Click(object sender, EventArgs e)
        {
            if (f == 2)
            {
                if (txttenkh.Text == "" || dtngaysinh.Text == "" || cbbgioitinh.Text == "" || txtsdt.Text == "" || txtcmnd.Text == "" || txtmap.Text == "" || dtngaydat.Text == "")
                {
                    MessageBox.Show("Mời bạn nhập thông tin", "Thông báo");
                    txttenkh.Focus();
                }
                else
                {
                    foreach (ListViewItem it in lvwkh.SelectedItems)
                    {
                        khachhang_DTO kh = new khachhang_DTO();
                        kh.makh = lblkh.Text;
                        kh.map = int.Parse(txtmap.Text);
                        kh.tenkh = txttenkh.Text;
                        kh.ngaysinh = DateTime.Parse(dtngaysinh.Text);
                        kh.gioitinh = cbbgioitinh.Text;
                        kh.sdt = txtsdt.Text;
                        kh.cmnd = int.Parse(txtcmnd.Text);
                        kh.ngaydat = DateTime.Parse(dtngaydat.Text);
                        if (khachhang_BUS.suakh(kh) == true)
                        {
                            MessageBox.Show("Sửa thành công");
                            lammoi();
                            trangthaitextbox(true);
                            nutluukh.Enabled = false;
                            nutluukh.BackColor = Color.Black;
                            dtngaysinh.Enabled = false;
                            dtngaydat.Enabled = false;
                            cbbgioitinh.Enabled = false;
                            ds = khachhang_BUS.laythongtinkh();
                            hienthilistview(ds, lvwkh);
                        }
                        else
                            MessageBox.Show("Sửa ko thành công");
                    }
                }
            }
        }

        private void nutxoakh_Click(object sender, EventArgs e)
        {
           
            foreach (ListViewItem it in lvwkh.SelectedItems)
            {
                phong_DTO p = new phong_DTO();
                kh.makh = it.Text;
                p.map = int.Parse(it.SubItems[1].Text);
                if (it != null)
                {
                    DialogResult d = MessageBox.Show("Bạn có muốn xóa?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (d == DialogResult.Yes)
                    {
                        try
                        {
                            if (khachhang_BUS.xoakh(kh,p) == true)
                            {
                                MessageBox.Show("Xóa thành công");
                                QLKH_Load(sender, e);
                                lammoi();
                                //p.tinhtrang = false;
                                nutsuakh.Enabled = false;
                                nutsuakh.BackColor = Color.Black;
                                nutxoakh.Enabled = false;
                                nutxoakh.BackColor = Color.Black;
                                ds = khachhang_BUS.laythongtinkh();
                                hienthilistview(ds, lvwkh);
                            }
                            else
                                MessageBox.Show("Xóa ko thành công");
                        }
                        catch
                        {
                            MessageBox.Show("Hết dữ liệu");
                        }
                    }
                }
            }
        }

        private void nutthoat_Click(object sender, EventArgs e)
        {
            GDChinh frm = new GDChinh();
            DialogResult d = MessageBox.Show("Bạn Có Muốn Thoát", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (d == DialogResult.Yes)
            {
                this.Close();
                frm.Show();
                
            }
        }

        private void nutquaylai_Click(object sender, EventArgs e)
        {
            QLKH_Load(sender, e);
            lvwkh.Enabled = true;
        }

        private void nuttim_Click(object sender, EventArgs e)
        {
            khachhang_DTO kh = new khachhang_DTO();
            DataSet ds = new DataSet();
            kh.tenkh = txttimkiem.Text;
            ds = khachhang_DAO.timkiemkhachhangtheoten(kh);
            hienthilistview(ds,lvwkh);
        }

        private void lvwkh_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                ListViewItem it = new ListViewItem();
                it = lvwkh.GetItemAt(e.X, e.Y);
                lblkh.Text = it.Text;
                txtmap.Text = it.SubItems[1].Text;
                txttenkh.Text = it.SubItems[2].Text;
                dtngaysinh.Text = it.SubItems[3].Text;
                cbbgioitinh.Text = it.SubItems[4].Text;
                txtsdt.Text = it.SubItems[5].Text;
                txtcmnd.Text = it.SubItems[6].Text;
                dtngaydat.Text = it.SubItems[7].Text;
                nutsuakh.Enabled = true;
                nutsuakh.BackColor = Color.Green;
                nutxoakh.Enabled = true;
                nutxoakh.BackColor = Color.Green;
            }
            catch
            {
                MessageBox.Show("Hết dữ liệu");
            }
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
